﻿using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class SetMManager : Singleton<SetMManager> {
#region 获取该界面的所有控件
    private Button[] open=new Button[2];//获取两个是否开启的按钮
    private Slider[] sdVolume=new Slider[2];//获取两个调节音量大小的滑动条
    private GameObject[] musicSprite = new GameObject[2];
    private GameObject[] soundSprite = new GameObject[2];
   private Button returnButton;//获取返回按钮
    private Button returnCpButton;//获取返回选关界面
    private Button restart; 
#endregion
    private bool saveSuccess;
    private ObtainMusic om;
    // Use this for initialization
    void Start () {
        int i = 0;
        Init();
        AddClik();
        if (!File.Exists(Application.persistentDataPath + @"/MIF.yzc"))
        {
            print("不存在");
            TextClass.Instance.Save(TextClass.Instance.Creat(true), "MIF.yzc");
        }//判断文件是否存在
        
        foreach(MusicInform mi in MusicManager.Instance.array)
        {
            if (i == 0)
                Modify(mi, musicSprite, sdVolume[i]);
            else Modify(mi, soundSprite, sdVolume[i]);
            i++;
        }
        //if (SceneManager.GetActiveScene().buildIndex <=2)
        //{
        //    GameObject go = GameObject.Find("Canvas/Set/SoundPlayer");
        //    om = go.GetComponent<ObtainMusic>();
        //}
        //else if( PropManager.Instance.msPlayer != null) om = PropManager.Instance.msPlayer;
        GameObject go = GameObject.Find("Canvas/Set/SoundPlayer");
        om = go.GetComponent<ObtainMusic>();
    }
	/// <summary>
    /// 改变播放的音乐
    /// </summary>
	private void MVoiceChange()
    {
        MusicManager.Instance.MusicValue = sdVolume[0].value;
    }
    private void SVoiceChange()
    {
        MusicManager.Instance.SoundValue = sdVolume[1].value;
    }

    private void Init()
    {
        open[0] = GameObject.Find("Set/MusicSlider/Music").GetComponent<Button>();
        musicSprite[0] = GameObject.Find("Set/MusicSlider/Music/OpenMusic");
        musicSprite[1] = GameObject.Find("Set/MusicSlider/Music/StopMusic");
        open[1] = GameObject.Find("Set/SoundSlider/Sound").GetComponent<Button>();
        soundSprite[0] = GameObject.Find("Set/SoundSlider/Sound/OpenSound");
        soundSprite[1] = GameObject.Find("Set/SoundSlider/Sound/StopSound");
        sdVolume[0] = GameObject.Find("Set/MusicSlider").GetComponent<Slider>();
        sdVolume[1] = GameObject.Find("Set/SoundSlider").GetComponent<Slider>();
        returnButton = GameObject.Find("Set/ReturnGame").GetComponent<Button>();
        
        if (GameObject.Find("Set/ReturnCpPanel"))
        {
            returnCpButton = GameObject.Find("Set/ReturnCpPanel").GetComponent<Button>();
            returnCpButton.onClick.AddListener(PropManager.Instance.ReturnCp);
        }
        if (GameObject.Find("Set/Restart"))
        {
            restart = transform.GetChild(6).GetComponent<Button>();
            restart.onClick.AddListener(PropManager.Instance.Restart);
        }
    }//获取该界面的所有组件
    private void AddClik()
    {
        returnButton.onClick.AddListener(Return);
        open[0].onClick.AddListener(MusicOpen);
        open[1].onClick.AddListener(SoundOpen);
        sdVolume[0].onValueChanged.AddListener(delegate { MVoiceChange(); });
        sdVolume[1].onValueChanged.AddListener(delegate { SVoiceChange(); });
    }//监听按钮点击事件
    private void Modify(MusicInform mf,GameObject[] a,Slider s)
    {
        if(mf.IsOpen)
        {
            a[0].SetActive(true);
            a[1].SetActive(false);
        }
        else
        {
            a[1].SetActive(true);
            a[0].SetActive(false);
            s.interactable = false;
        }
        s.value = mf.Volume;
    }
    
    #region 按钮点击事件
        private void MusicOpen()
        {
            PlayMusic(0);
            if (musicSprite[0].activeSelf)
            {
                musicSprite[0].SetActive(false);
                musicSprite[1].SetActive(true);
                MusicManager.Instance.IsNStopM=false;
                sdVolume[0].interactable = false;
            }
            else
            {
                 musicSprite[1].SetActive(false);
                musicSprite[0].SetActive(true);
                MusicManager.Instance.IsNStopM = true;
                sdVolume[0].interactable = true;
            }
        }//是否打开音乐
        private void SoundOpen()
        {
            PlayMusic(0);
            if (soundSprite[0].activeSelf)
            {
                soundSprite[0].SetActive(false);
                soundSprite[1].SetActive(true);
                MusicManager.Instance.IsNStopS = false;
                sdVolume[1].interactable = false;
            }
            else
            {
                soundSprite[1].SetActive(false);
                soundSprite[0].SetActive(true);
                MusicManager.Instance.IsNStopS = true;
                sdVolume[1].interactable = true;
            }
        }//是否打开音效
        
        private void Return()
        {
            PlayMusic(0);
            Time.timeScale = 1;
            UIshower.Instance.RecPlay(Nodeal.SetMusic);
            Preservation();
        }//返回
        private void Preservation()
        {
            PlayMusic(0);
            bool isOpen1,isOpen2;
            if (musicSprite[0].activeSelf == true) isOpen1 = true;
            else isOpen1 = false;
            if (soundSprite[0].activeSelf == true) isOpen2 = true;
            else isOpen2 = false;
            TextClass.Instance.MFLoad(false,out saveSuccess,isOpen1,isOpen2,sdVolume[0].value,sdVolume[1].value);
        }//保存

    public override void FixedAwake(){}
    public void PlayMusic(int index)
    {
        if (om != null)
            om.ChangeClip(om.musicGather[index]);
    }
    #endregion
}
